﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsForms合并
{
   
    public partial class Form1 : Form
    { 
        private string doc1;
        private string doc2;
        private string doc;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                FileStream fin1 = new FileStream(this.doc1, FileMode.Open, FileAccess.Read);
                FileStream fin2 = new FileStream(this.doc2, FileMode.Open, FileAccess.Read);
                FileStream fout = new FileStream(this.doc, FileMode.Create, FileAccess.Write);


                using (StreamReader reader1 = new StreamReader(fin1, Encoding.UTF8))
                {
                    using (StreamReader reader2 = new StreamReader(fin2, Encoding.UTF8))
                    {
                        using (StreamWriter writer = new StreamWriter(fout, Encoding.UTF8))
                        {
                            string s = null;
                            while ((reader1.ReadLine()) != null)
                            {
                                s = reader1.ReadLine();
                                writer.WriteLine(s);
                            }
                            while ((reader2.ReadLine()) != null)
                            {
                                s = reader2.ReadLine();
                                writer.WriteLine(s);
                            }
                            MessageBox.Show("合并成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine($"file{ex.FileName}does not exist.");
                MessageBox.Show("合并失败，找不到文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex1)
            {
                MessageBox.Show("合并失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Console.WriteLine(ex1);
            }
          
        }

     

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog saveFile = new OpenFileDialog();
            saveFile.Filter = "所有txt文件|*.txt";//设置文件类型
            saveFile.Title = "导出数据";//设置标题

            saveFile.AddExtension = true;//是否自动增加所辍名
            saveFile.AutoUpgradeEnabled = true;//是否随系统升级而升级外观
            if (saveFile.ShowDialog() == DialogResult.OK)//如果点的是确定就得到文件路径
            {
                this.textBox2.Text = saveFile.FileName;//得到文件路径
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog saveFile = new OpenFileDialog();
            saveFile.Filter = "所有txt文件|*.txt";//设置文件类型
            saveFile.Title = "导出数据";//设置标题

            saveFile.AddExtension = true;//是否自动增加所辍名
            saveFile.AutoUpgradeEnabled = true;//是否随系统升级而升级外观
            if (saveFile.ShowDialog() == DialogResult.OK)//如果点的是确定就得到文件路径
            {
                this.textBox1.Text = saveFile.FileName;//得到文件路径
            }
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.doc1 = textBox1.Text;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            this.doc2 = textBox2.Text;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            this.doc = @"Data\" + newfilename.Text;
        }
    }
}
